﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MM.Common.DBBase;

namespace MM.Common.Base
{
    public static class Times
    {
        // Methods
        public static string GetAge(this DateTime day)
        {
            int betweenDays = day.GetBetweenDays(DateTime.Now);
            if (betweenDays <= 0x1f)
            {
                return (betweenDays + "天");
            }
            int betweenMonths = day.GetBetweenMonths(DateTime.Now);
            if (betweenMonths <= 12)
            {
                return (betweenMonths + "月");
            }
            return (day.GetBetweenYears(DateTime.Now) + "岁");
        }

        public static string GetAgeInfo(DateTime birthday)
        {
            DateTime dBTime = SystemInfo.DbData.GetDBTime();
            return GetAgeInfo(birthday, dBTime);
        }

        public static string GetAgeInfo(DateTime birthday, DateTime today)
        {
            if (birthday != DateTime.MinValue)
            {
                DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("birthday", birthday), new InputDataParam("cal_date", today) });
                object obj2 = new object();
                if (SystemInfo.DbData.ExecuteScalar("SELECT F_GET_AGE_INFO(:birthday ,:cal_date) FROM DUAL", parms, ref obj2))
                {
                    return obj2.ToString();
                }
            }
            return "";
        }

        public static int GetBetweenDays(this DateTime day, DateTime end_day)
        {
            TimeSpan span = (TimeSpan)(day - end_day);
            return span.Days;
        }

        public static int GetBetweenMonths(this DateTime day, DateTime end_day)
        {
            int months = 0;
            if (day >= end_day)
            {
                while (day.AddMonths(months) < end_day)
                {
                    months--;
                }
                return months;
            }
            while (day.AddMonths(months) > end_day)
            {
                months++;
            }
            return months;
        }

        public static int GetBetweenYears(this DateTime day, DateTime end_day)
        {
            int num = 0;
            if (day >= end_day)
            {
                while (day.AddYears(num) > end_day)
                {
                    num--;
                }
                return num;
            }
            while (day.AddYears(num) > end_day)
            {
                num++;
            }
            return num;
        }

        public static DateTime GetLastDateForMonth(this DateTime day)
        {
            int year = day.Year;
            int month = day.Month;
            int num3 = DateTime.DaysInMonth(year, month);
            DateTime time = Convert.ToDateTime(string.Concat(new object[] { year.ToString(), "-", month.ToString(), "-", num3 }));
            time.AddDays(1.0).AddMilliseconds(-1.0);
            return time;
        }
    }
}
